
<script>
import { mapState, } from 'vuex';
export default {
  name: 'OptionsMenu',
  props: {
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      base: [
        { text: '终止', code: 'end', },
        {
          text: '转工单',
          code: 'toOrder',
        }
      ],
    };
  },
  computed: {
    ...mapState('login', ['userInfo']),
    // 当前登录账号是否是责任人
    isLeader() {
      // PS: 任务的处理 只能由当前的任务的负责人处理，该任务的上级的负责人也不能处理
      // return [(this.data.leaderUser), this.data.parentLeaderId].indexOf(Number(this.userInfo.id)) > -1
      //   ? 'leader'
      //   : '';
      return [this.data.leaderUser].indexOf(Number(this.userInfo.id)) > -1
        ? 'leader'
        : '';
    },
    // 当前登录账号是否是参与人员
    isJoinUser() {
      return this.data.partUser.join().includes(String(this.userInfo.id))
        ? 'join'
        : '';
    },
    // 当前登录账号是否是创建人员

    isCreateUser() {
      return Number(this.data.createUser) === Number(this.userInfo.id)
        ? 'create'
        : '';
    },
    // 当前登录账号是否是审核人
    isAuditUser() {
      return Number(this.data.auditUser) === Number(this.userInfo.id)
        ? 'audit'
        : '';
    },
    regText() {
      const all = [this.isLeader, this.isJoinUser, this.isCreateUser, this.isAuditUser].filter((e) => e);
      const allText = all.join('|');
      return allText;
    },
    renderItem() {
      let text = {};

      // //状态(0待提交1待处理2待审核3已驳回4已暂停5已停止6已完成)
      switch (String(this.data.states)) {
        // 待提交
        case '0':
          if (this.data.child) {
            text = [
              {
                text: '编辑',
                code: 'edit',
                permissonCode: ['create', 'leader'],
              },
              {
                text: '删除',
                code: 'del',
                permissonCode: ['create', 'leader'],
              }
            ];
          } else {
            text = [
              {
                text: '编辑',
                code: 'edit',
                permissonCode: ['create', 'leader'],
              },
              {
                text: '删除',
                code: 'del',
                permissonCode: ['create', 'leader'],

              },
              { text: '提交',
                code: 'submit',
                permissonCode: ['leader'],
              }
            ];
          }

          break;
        //  待处理
        case '1':
          // 存在子级
          // 1.判断 是否存在 子级 且 当前 是否是父节点  默认存在子级
          //

          if (this.data.child && this.data.parentId === undefined) {
            text = [
              {
                text: '添加子任务',
                code: 'addChild',
                permissonCode: ['create', 'leader'],
              },

              {
                text: '终止',
                code: 'end',
                permissonCode: ['create', 'leader'],

              }];
            // 在处理中

            if (!this.data.compRate) {
              text.push({
                text: '编辑',
                code: 'edit',
                permissonCode: ['create', 'leader'],
              },);
            }
          } else if (!this.data.child && this.data.parentId === undefined) {
            text = [{
              text: '处理',
              code: 'handle',
              permissonCode: ['leader', 'join'],

            }, {
              text: '添加子任务',
              code: 'addChild',
              permissonCode: ['leader', 'create'],

            },
            {
              text: '暂停',
              code: 'stop',
              permissonCode: ['leader'],

            },
            {
              text: '终止',
              code: 'end',
              permissonCode: ['leader', 'create'],

            },
            // {
            //   text: '编辑',
            //   code: 'edit',
            //   permissonCode: ['create', 'leader'],
            // },
            {
              text: '转交',
              code: 'passed',
              permissonCode: ['leader'],

            }];
            if (!this.data.compRate) {
              text.push({
                text: '编辑',
                code: 'edit',
                permissonCode: ['create', 'leader'],
              },);
            }
          } else if (this.data.child === undefined && this.data.parentId) {
            text = [{
              text: '处理',
              code: 'handle',
              permissonCode: ['leader', 'join'],

            }, {
              text: '暂停',
              code: 'stop',
              permissonCode: ['leader'],

            },
            // {
            //   text: '编辑',
            //   code: 'edit',
            //   permissonCode: ['leader', 'create'],
            // },
            {
              text: '终止',
              code: 'end',
              permissonCode: ['leader', 'create'],

            },
            {
              text: '转交',
              code: 'passed',
              permissonCode: ['leader'],

            }];
            if (!this.data.compRate) {
              text.push({
                text: '编辑',
                code: 'edit',
                permissonCode: ['create', 'leader'],
              },);
            }
          }

          break;
        // 待审核
        case '2':
          if (this.data.parentId) {
            text = [];
          } else {
            text = [{
              text: '审核',
              code: 'review',
              permissonCode: ['audit'],
            }
            //  {
            //   text: '查看',
            //   code: 'search',
            //   permissonCode: ['all'],
            // }
            ];
          }

          break;

        // 以驳回
        case '3':
          // if (this.data.child) {
          //   text = [{
          //     text: '编辑',
          //     code: 'edit',
          //     permissonCode: ['create', 'leader'],

          //   }];
          // } else {
          //   text = [{
          //     text: '处理',
          //     code: 'handle',
          //     permissonCode: ['leader', 'join'],

          //   }, {
          //     text: '终止',
          //     code: 'end',
          //     permissonCode: ['create', 'leader'],

          //   },
          //   {
          //     text: '暂停',
          //     code: 'stop',
          //     permissonCode: ['leader'],

          //   },
          //   {
          //     text: '转交',
          //     code: 'passed',
          //     permissonCode: ['leader'],

          //   }];
          // }
          text = [{
            text: '处理',
            code: 'handle',
            permissonCode: ['leader', 'join'],

          }, {
            text: '终止',
            code: 'end',
            permissonCode: ['create', 'leader'],

          },
          {
            text: '暂停',
            code: 'stop',
            permissonCode: ['leader'],

          },
          {
            text: '转交',
            code: 'passed',
            permissonCode: ['leader'],

          }];
          break;
        case '4':
          text = [{
            text: '开启',
            code: 'open',
            permissonCode: ['leader'],
          }];
          break;
        // 终止
        case '5':
          text = [];
          break;
        // 完成
        case '6':
          text = [];
          break;
        default:
          text = [];
      }
      return text;
    },
  },
  methods: {
    emitClick(v) {
      this.$emit('click', v);
    },
  },
  render(h) {
    const renderPermissionsButtons = [
      {
        text: '查看',
        code: 'search',
      }
    ];

    for (const i of this.renderItem) {
      let val = null;
      if (i.permissonCode.includes(this.isLeader)) {
        val = i;
      } else if (i.permissonCode.includes(this.isCreateUser)) {
        val = i;
      } else if (i.permissonCode.includes(this.isJoinUser)) {
        val = i;
      } else if (i.permissonCode.includes(this.isAuditUser)) {
        val = i;
      }
      if (val) {
        renderPermissionsButtons.push(val);
      }

      // if (new RegExp(this.regText).test(i.permissonCode.join(','))) {
      //   renderPermissionsButtons.push(i);
      // }
    }
    const baseButtons = renderPermissionsButtons.slice(0, 2);
    const downItemButtons = renderPermissionsButtons.slice(2);
    let dropdown = [];

    const _b = baseButtons.map((e) => {
      return h('el-button', {
        props: {
          type: 'text',
        },
        domProps: {
          innerHTML: e.text,
        },
        on: {
          click: () => {
            this.emitClick(e.code);
          },
        },
      });
    });
    if (downItemButtons.length) {
      const downs = downItemButtons.map((e) => {
        return h('el-dropdown-item', {
          domProps: {
            innerHTML: e.text,
          },
          attrs: {
            command: e.code,
          },
        });
      });
      dropdown = h(
        'el-dropdown',
        {
          on: {
            command: (v) => {
              this.emitClick(v);
            },
          },
          style: {
            'margin-left': '10px',
          },
        },
        [
          h('el-button', {
            props: {
              type: 'text',
            },
            domProps: {
              innerHTML: '更多 <i   class="el-icon-arrow-down "></i>',
            },
          }),
          h(
            'el-dropdown-menu',
            {
              slot: 'dropdown',
            },
            downs
          )
        ]
      );
    }

    return h('div', [_b, dropdown]);
  },
};
</script>

<style>
</style>
